package com.mh.mh.tool;

import com.mh.mh.entity.GuaEntity;
import com.mh.mh.entity.GuaResutEntity;
import org.springframework.stereotype.Component;

/**
 * @author Europe
 */
@Component
public class BaGuaUtil {

    /**
     * 推演
     * @param num 先天数
     * @return 卦象
     */
    public String tuiYan(int num){

        // 卦象
        String guaXiang = "";

        if(num == 1){
            // 乾金
            guaXiang = "111";
        }
        else if(num == 2){
            // 兑金
            guaXiang = "011";
        }
        else if(num == 3){
            // 离火
            guaXiang = "101";
        }
        else if(num == 4){
            // 震木
            guaXiang = "001";
        }
        else if(num == 5){
            // 巽木
            guaXiang = "110";
        }
        else if(num == 6){
            // 坎水
            guaXiang = "010";
        }
        else if(num == 7){
            // 艮土
            guaXiang = "100";
        }
        else if(num == 8){
            // 坤土
            guaXiang = "000";
        }

        return guaXiang;
    }


    public String getBianGua(int dongYu, String gua){

        if(dongYu == 1){
            String guaIndex = gua.charAt(2)+"";
            guaIndex = guaChange(guaIndex);
            gua = gua.substring(0,1) + gua.substring(1,2) + guaIndex;
        }
        else if(dongYu == 2){
            String guaIndex = gua.substring(1,2);
            guaIndex = guaChange(guaIndex);
            gua = gua.substring(0,1) + guaIndex + gua.substring(2);
        }
        else {
            String guaIndex = gua.substring(0,1);
            guaIndex = guaChange(guaIndex);
            gua = guaIndex + gua.substring(1,2) + gua.substring(2);
        }
        return gua;
    }

    public static void main(String[] args) {

        String guaIndex = "1";
        String gua = "000";
        String c = gua.charAt(0)+"";
        String c1 = gua.charAt(1)+"";
        System.out.println(gua.substring(2));
        System.out.println(gua.substring(1));
        String aaa = gua.substring(0,1) + gua.substring(1,2) + guaIndex;
        System.out.println(aaa);
    }


    public String guaChange(String guaIndex){
        if("0".equals(guaIndex)){
            guaIndex = "1";
        }else {
            guaIndex = "0";
        }
        return guaIndex;
    }


    /**
     * 断卦
     */
    public GuaResutEntity duanGua(GuaEntity zhuGuaEntity, GuaEntity huGuaEntity, GuaEntity bianGuaEntity){
        GuaResutEntity guaResutEntity = new GuaResutEntity();
        String startTxt = getGuaTxt(zhuGuaEntity);
        String developmentTxt = getGuaTxt(huGuaEntity);
        String endTxt = getGuaTxt(bianGuaEntity);
        guaResutEntity.setStartTxt(startTxt);
        guaResutEntity.setDevelopmentTxt(developmentTxt);
        guaResutEntity.setEndTxt(endTxt);
        return guaResutEntity;
    }


    /**
     * 五行断吉凶
     * @param guaEntity
     * @return
     */
    public String getGuaTxt(GuaEntity guaEntity){

        String sk = "";

        for(int i = 0; i<5; i++){
            String s = "";
            if(i == 0){
                s = muGua(guaEntity);
            }
            else if(i == 1){
                s = huoGua(guaEntity);
            }
            else if(i == 2){
                s = tuGua(guaEntity);
            }
            else if(i == 3){
                s = jinGua(guaEntity);
            }
            else {
                s = shuiGua(guaEntity);
            }

            if(!s.equals("")){
                sk = s;
                break;
            }
        }

        return sk;
    }

    /**
     * 木卦
     * @param guaEntity
     * @return
     */
    public String muGua(GuaEntity guaEntity){
        String sk = "";

        if("木".equals(guaEntity.getUpWuXing()) && "土".equals(guaEntity.getDownWuXing()) ){
            sk = "木克土";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小吉";
            }else {
                sk = sk+",大凶";
            }
        }
        else if("木".equals(guaEntity.getUpWuXing()) && "火".equals(guaEntity.getDownWuXing()) ){
            sk = "木生火";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小凶";
            }else {
                sk = sk+",大吉";
            }
        }
        else if("木".equals(guaEntity.getUpWuXing()) && "金".equals(guaEntity.getDownWuXing()) ){
            sk = "金克木";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大凶";
            }else {
                sk = sk+",小吉";
            }
        }
        else if("木".equals(guaEntity.getUpWuXing()) && "水".equals(guaEntity.getDownWuXing()) ){
            sk = "水生木";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大吉";
            }else {
                sk = sk+",小凶";
            }
        }
        else if("木".equals(guaEntity.getUpWuXing()) && "木".equals(guaEntity.getDownWuXing()) ){
            sk = "木木，大吉";
        }

        return sk;
    }

    /**
     * 火卦
     * @param guaEntity
     * @return
     */
    public String huoGua(GuaEntity guaEntity){
        String sk = "";

        if("火".equals(guaEntity.getUpWuXing()) && "金".equals(guaEntity.getDownWuXing()) ){
            sk = "火克金";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小吉";
            }else {
                sk = sk+",大凶";
            }
        }
        else if("火".equals(guaEntity.getUpWuXing()) && "土".equals(guaEntity.getDownWuXing()) ){
            sk = "火生土";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小凶";
            }else {
                sk = sk+",大吉";
            }
        }
        else if("火".equals(guaEntity.getUpWuXing()) && "水".equals(guaEntity.getDownWuXing()) ){
            sk = "水克火";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大凶";
            }else {
                sk = sk+",小吉";
            }
        }
        else if("火".equals(guaEntity.getUpWuXing()) && "木".equals(guaEntity.getDownWuXing()) ){
            sk = "木生火";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大吉";
            }else {
                sk = sk+",小凶";
            }
        }
        else if("火".equals(guaEntity.getUpWuXing()) && "火".equals(guaEntity.getDownWuXing()) ){
            sk = "火火，大吉";
        }

        return sk;
    }

    /**
     * 土卦
     * @param guaEntity
     * @return
     */
    public String tuGua(GuaEntity guaEntity){
        String sk = "";

        if("土".equals(guaEntity.getUpWuXing()) && "水".equals(guaEntity.getDownWuXing()) ){
            sk = "土克水";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小吉";
            }else {
                sk = sk+",大凶";
            }
        }
        else if("土".equals(guaEntity.getUpWuXing()) && "金".equals(guaEntity.getDownWuXing()) ){
            sk = "土生金";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小凶";
            }else {
                sk = sk+",大吉";
            }
        }
        else if("土".equals(guaEntity.getUpWuXing()) && "木".equals(guaEntity.getDownWuXing()) ){
            sk = "木克土";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大凶";
            }else {
                sk = sk+",小吉";
            }
        }
        else if("土".equals(guaEntity.getUpWuXing()) && "火".equals(guaEntity.getDownWuXing()) ){
            sk = "火生土";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大吉";
            }else {
                sk = sk+",小凶";
            }
        }
        else if("土".equals(guaEntity.getUpWuXing()) && "土".equals(guaEntity.getDownWuXing()) ){
            sk = "土土，大吉";
        }

        return sk;
    }

    /**
     * 金卦
     * @param guaEntity
     * @return
     */
    public String jinGua(GuaEntity guaEntity){
        String sk = "";

        if("金".equals(guaEntity.getUpWuXing()) && "木".equals(guaEntity.getDownWuXing()) ){
            sk = "金克木";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小吉";
            }else {
                sk = sk+",大凶";
            }
        }
        else if("金".equals(guaEntity.getUpWuXing()) && "水".equals(guaEntity.getDownWuXing()) ){
            sk = "金生水";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小凶";
            }else {
                sk = sk+",大吉";
            }
        }
        else if("金".equals(guaEntity.getUpWuXing()) && "火".equals(guaEntity.getDownWuXing()) ){
            sk = "火克金";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大凶";
            }else {
                sk = sk+",小吉";
            }
        }
        else if("金".equals(guaEntity.getUpWuXing()) && "土".equals(guaEntity.getDownWuXing()) ){
            sk = "土生金";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大吉";
            }else {
                sk = sk+",小凶";
            }
        }
        else if("金".equals(guaEntity.getUpWuXing()) && "金".equals(guaEntity.getDownWuXing()) ){
            sk = "金金，大吉";
        }

        return sk;
    }


    /**
     * 水卦
     * @param guaEntity
     * @return
     */
    public String shuiGua(GuaEntity guaEntity){
        String sk = "";

        if("水".equals(guaEntity.getUpWuXing()) && "火".equals(guaEntity.getDownWuXing()) ){
            sk = "水克火";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小吉";
            }else {
                sk = sk+",大凶";
            }
        }
        else if("水".equals(guaEntity.getUpWuXing()) && "木".equals(guaEntity.getDownWuXing()) ){
            sk = "水生木";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",小凶";
            }else {
                sk = sk+",大吉";
            }
        }
        else if("水".equals(guaEntity.getUpWuXing()) && "土".equals(guaEntity.getDownWuXing()) ){
            sk = "土克水";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大凶";
            }else {
                sk = sk+",小吉";
            }
        }
        else if("水".equals(guaEntity.getUpWuXing()) && "金".equals(guaEntity.getDownWuXing()) ){
            sk = "金生水";
            if(guaEntity.getTiyong().equals("downYao")){
                sk = sk+",大吉";
            }else {
                sk = sk+",小凶";
            }
        }
        else if("水".equals(guaEntity.getUpWuXing()) && "水".equals(guaEntity.getDownWuXing()) ){
            sk = "水水，大吉";
        }

        return sk;
    }



    public void setWuXing(GuaEntity guaEntity){

        if("111".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("金");
        }
        else if("011".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("金");
        }
        else if("101".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("火");
        }
        else if("001".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("木");
        }
        else if("110".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("木");
        }
        else if("010".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("水");
        }
        else if("100".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("土");
        }
        else if("000".equals( guaEntity.getUpYao())){
            guaEntity.setUpWuXing("土");
        }




        if("111".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("金");
        }
        else if("011".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("金");
        }
        else if("101".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("火");
        }
        else if("001".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("木");
        }
        else if("110".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("木");
        }
        else if("010".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("水");
        }
        else if("100".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("土");
        }
        else if("000".equals( guaEntity.getDownYao())){
            guaEntity.setDownWuXing("土");
        }

    }






}
